package com.lottery.game.controller.v2.pay;

import com.lottery.game.common.Response;
import com.lottery.game.common.constants.GlobalLotteryConstants;
import com.lottery.game.common.exception.BaseException;
import com.lottery.game.constants.RespErrorCodes;
import com.lottery.game.controller.v2.find.request.MessageCodeTO;
import com.lottery.game.controller.v2.pay.requestVO.FindPasswordVO;
import com.lottery.game.controller.v2.pay.requestVO.FindPayPasswordTO;
import com.lottery.game.controller.v2.pay.requestVO.FindPayPasswordVO;
import com.lottery.game.controller.v2.pay.requestVO.MessageCodeVO;
import com.lottery.game.core.modules.entity.user.GameUser;
import com.lottery.game.core.modules.service.app.GameUserService;
import com.lottery.game.core.modules.service.app.v2.MessageSendAndCheckService;
import com.lottery.game.core.validator.annotation.Validate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


/**
 * @Description 用户支付密码Controller
 * @Classname UserPayController
 * @Date 2019/4/25 13:40
 * @Author liusc <liushicheng1993@vip.qq.com>
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/v2/userPay")
@Api(tags = {"APP-用户找回支付密码"})
@Slf4j
public class UserPayController {

    @Autowired
    private MessageSendAndCheckService messageSendAndCheckService;

    @Autowired
    private GameUserService gameUserService;

    @ApiOperation(value = "发送找回支付密码手机验证码API")
    @ApiImplicitParam(name = "phone", value = "手机号码",dataType = "String",example = "15348304228",required = true)
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    public Response<String> send(@RequestParam("phone") String phone) {
        try {
            //找回密码之前，判断是否注册过
            GameUser gameUser = gameUserService.getGameUserByUserName(phone);
            String resultStr = "请求成功";
            if (gameUser == null) {
                return Response.buildError("该手机号尚未注册");
            }
            //int i = messageSendAndCheckService.sendMessageByMobile(phone, "找回支付密码");
            return Response.build(messageSendAndCheckService.send(phone, "找回支付密码"));
            //判断是否成功
            /*if (GlobalLotteryConstants.RES_SUCCESS == i){
                return Response.buildSuccess(resultStr);
            }else {
                resultStr = "请求失败";
                return Response.buildError(resultStr);
            }*/
        }catch (Exception e){
        log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
            return Response.buildError("操作失败!稍后再试");
        }
    }
    @ApiOperation(value = "校验code通过手机和codeAPI", notes = "body参数:\\n{\\n\" + \"    \\\"code\\\": \\\"5209\\\",\\n\" + \" \\\"phone\\\": \\\"15348304228\\\",\\n\" + \"  \\\"token\\\": \\\"eyJzaWduIjoiNGU2MDE1ZGVmMjY2N2NkOWQyMzgxZGIzZjcyMjRiNDIiLCJ0aW1lc3RhbXAiOiIxNTU1Mjk5MDYyNTE5In0\\\"\\n\" + \"}")
    @RequestMapping(value = "/validate", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Validate
    public Response<Boolean> validate(@RequestBody MessageCodeTO param) {

        try {
            //2.校验code
            messageSendAndCheckService.check(param.getToken(), param.getCode(), param.getPhone());
            return Response.buildSuccess("验证成功！");
        }catch (Exception e){
            log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
            return Response.buildError("操作失败!稍后再试");
        }
    }

    @ApiOperation(value = "重置支付密码API", notes = "body参数:\n{\n" + "    \"code\": \"5209\",\n" + "    \"password\": \"123456\",\n" + "    \"phone\": \"15348304228\",\n" + "    \"rePassword\": \"123456\",\n" + "    \"token\": \"eyJzaWduIjoiNGU2MDE1ZGVmMjY2N2NkOWQyMzgxZGIzZjcyMjRiNDIiLCJ0aW1lc3RhbXAiOiIxNTU1Mjk5MDYyNTE5In0\"\n" + "}")
    @RequestMapping(value = "/password/reset", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Validate
    public Response<Boolean> reset(@RequestBody FindPayPasswordTO param) {

        try {
            //1.判断2次密码是否一致
            if (!param.getPassword().equals(param.getRePassword())) {
                return Response.buildError("两次登陆密码不一致");
            }
            //1.5判断密码是否为数字六位
            if(param.getPassword().length()==6){
                try {
                    int a = Integer.valueOf(param.getPassword());
                }catch (Exception e){
                    return Response.buildError("支付密码必须为6位的数字");
                }
            }else{
                return Response.buildError("支付密码必须为6位的数字");
            }
            messageSendAndCheckService.check(param.getToken(), param.getCode(), param.getPhone());
            //2.查询用户信息
            String phone = param.getPhone();
            GameUser gameUser = gameUserService.getGameUserByUserName(phone);
            //3.修改
            Boolean aBoolean = gameUserService.updateNewPayPwd(gameUser.getId(), gameUser.getUsername(), param.getPassword());
            if (aBoolean){
                return Response.buildSuccess("修改成功");
            }else {
               return Response.buildError("修改失败");
            }
        }catch (Exception e){
            log.error(RespErrorCodes.UNKNOWN_ERROR.errorCode(), e);
            return Response.buildError("操作失败!稍后再试");
        }

    }
}
